package buss.console.util;

import java.text.ParseException;
import java.text.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.base.exception.CurrencyRuntimeException;
import com.lifemall.redis.RedisUtil;

public class RedisHandle {
	 private static final Logger logger = Logger.getLogger("RedisHandle");
     public static List<String> queryStock(String month,String attractionId,String channel) {   	 
    	String[] str = new String[31];
 		for(int i=0;i<31;i++)
 		{
 			str[i]=month+"-"+String.format("%02d", i+1);
 		}		
 		List<String> quantitys=null;
		try {
			quantitys = RedisUtil.getDisneySKU(0, attractionId,channel, str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
 		return quantitys;
    }
     
     public static Map<String,String> findTicketsStock(String startDate,String endDate,String attractionId,String channel){
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
 		Map<String,String> stock=new HashMap<String,String>();
 		List<Calendar> calList = getCalList(startDate,endDate);
        String[] str=new String[calList.size()];
        for (int i = 0; i < calList.size(); i++) {
        	str[i] = sdf.format(calList.get(i).getTime());
		}
        for (int i = 0; i < str.length; i++) {
        	logger.debug("入参：str"+str[i].toString());
		}
        List<String> quantitys=null;
		try {
			quantitys = RedisUtil.getDisneySKU(0, attractionId, channel, str);
			for (int i = 0; i < quantitys.size(); i++) {
				logger.debug("出参：quantitys"+quantitys.get(i).toString());
			}
		} catch (Exception e) {			
			logger.error(e);
		}        
		for(int i=0;i<calList.size();i++){
			if(quantitys != null) {
				stock.put(str[i], quantitys.get(i));
			}
		 }
 		
 		return stock;
 	}
     
 	/**
 	 * 
 	 * 获取两个日期字符串之间的所有日期集合 List<Calendar>
 	 * 
 	 * @param start
 	 * @param end
 	 * @return List<Calendar>
 	 */
 	public static List<Calendar> getCalList(String start,String end) {
 		List<Calendar> calList = new ArrayList<Calendar>();
 		Calendar startCal = getCalendar(start);
 		Calendar endCal = getCalendar(end);
 		int isBig = endCal.compareTo(startCal);
 		if(isBig<1){
 			Calendar temp = Calendar.getInstance();
 			temp = startCal ;
 			startCal = endCal;
 			endCal = temp;
 		}
 		int dayNum = (int)((endCal.getTime().getTime()-startCal.getTime().getTime())/(24*60*60*1000));
 		for (int i = 0; i <= dayNum; i++) {
 			Calendar calendar = Calendar.getInstance();
// 			int YY = CD.get(Calendar.YEAR) ;
// 			int MM = CD.get(Calendar.MONTH)+1;
// 			int DD = CD.get(Calendar.DATE);
// 			int HH = CD.get(Calendar.HOUR);
// 			int NN = CD.get(Calendar.MINUTE);
// 			int SS = CD.get(Calendar.SECOND);
// 			int MI = CD.get(Calendar.MILLISECOND);
 			int year = startCal.get(Calendar.YEAR);
 			int month = startCal.get(Calendar.MONTH);
 			int day = startCal.get(Calendar.DATE)+i;
 			calendar.set(year, month, day);
 			calList.add(calendar);
 		}
 		return calList;
 	}
 	/**
 	 * 
 	 * 将日期格式字符串yyyy-MM-dd转化成Calendar
 	 * 
 	 * @param date
 	 * @return Calendar
 	 * 
 	 * @throws RuntimeException
 	 */
 	public static Calendar getCalendar(String date){
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 		try {
 			sdf.parse(date);
 		} catch (ParseException e) {
 			throw new CurrencyRuntimeException("传入的日期格式不正确！");
 		}
 		Calendar calendar=Calendar.getInstance();
 		int num[] = new int[3];
 		String str[] = date.split("-");
 		for (int i = 0; i < str.length; i++) {
 			num[i] = Integer.parseInt(str[i].toString());
 		}
 		calendar.set(num[0], num[1]-1, num[2]);
 		return calendar;
 	}
}
